##
# Show slave status
# @param $1 install home
# @param $2 pattern
# @param $3 delimiter
# @param $4 host
# @param $5 port
# @param $6 username
# @param $7 password
#
use strict;
my $SQL="show slave status\\G";
my ($MYSQL_HOME,$RE,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");
my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin";
if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_error=Status${DELIM}Down\n";
    exit 1;
}
# Execute command and display the result as key-value pairs
my @KV=`$MYSQL -s $PARAM -e"$SQL"`;
shift(@KV);
chomp(@KV);
for (@KV) {
    my ($key, $value) = split /:/, $_;
    $key =~ s/^\s+//g;
    $key =~ s/\s+$//g;
    $value =~ s/^\s+//g;
    $value =~ s/\s+$//g;
    print "em_result=${key}${DELIM}${value}\n";
    if ("$key" eq "Master_Log_File" or "$key" eq "Relay_Master_Log_File") {
      my $tmp_no = $value;
      eval "\$tmp_no =~ $RE";
      print "em_result=${key}_no${DELIM}${tmp_no}\n";
    }
}
exit 0;

